package ch.unisi.inf.engine;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import ch.unisi.inf.engine.Image;

/**
 * 
 * @author Mohamed.Mosa@lu.unisi.ch
 * @version April 29th
 * 
 * The library class is responsible for holding all the images.
 * It has the load and save methods which we use for serializing 
 * and deserializing the images
 * 
 */

public class Library implements Serializable{
	ArrayList<Image> images;
	String name;
	static Image img = new Image(null, null);

	public Library(String name) {
		super();
		this.name = name;
		this.images = new ArrayList<Image>();
	}

	/**
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
 
	public int getSize(){
		return images.size();
	}

	/**
	 * @return library
	 */
	public ArrayList<Image> getLibrary() {
		return images;
	}
	
	public void add (final Image img) {
		images.add(img);
	}

	public void remove(final Image img){
		images.remove(img);
	}
	
	public void printImages() {
		for (final Image img : images) {
			System.out.println(img);
		}
	}

	public void save() throws IOException {
		//SERIALIZATION
		try {
			 FileOutputStream fileStream = new FileOutputStream("libraryFile.lib");
			 ObjectOutputStream os = new ObjectOutputStream(fileStream);
			 os.writeObject(img);
			 os.close();
		} 
		
		catch (FileNotFoundException e) {
				e.printStackTrace();
 		}	
	}
 
	public void load() throws IOException {
		//DESERIALIZATION
		try {
			 FileInputStream filestream = new FileInputStream("libraryFile.lib");
			 ObjectInputStream is = new ObjectInputStream(filestream);
			 Object one = is.readObject();
			 Image image = (Image) img;
			 System.out.println(image);
			 is.close();
			}
		
		catch(Exception e){
			  e.printStackTrace();
		}
	}
}
